package com.potenza.training.interview.controller;

import com.potenza.training.interview.entity.InterviewInvitationEntity;
import com.potenza.training.interview.feign.SysServiceFegin;
import com.potenza.training.interview.service.InterviewInvitationService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 面试邀请
 *
 * @author lihonggang
 * @email lihonggang@gmail.com
 * @date 2021-07-28 18:57:10
 */
@RestController
@RequestMapping("interview/interviewinvitation")
public class InterviewInvitationController {
    @Autowired
    private InterviewInvitationService interviewInvitationService;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    SysServiceFegin sysServiceFegin;
    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("interview:interviewinvitation:list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = interviewInvitationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("interview:interviewinvitation:info")
    public R info(@PathVariable("id") Long id){
		InterviewInvitationEntity interviewInvitation = interviewInvitationService.getById(id);

        return R.ok().put("interviewInvitation", interviewInvitation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("interview:interviewinvitation:save")
    public R save(@RequestBody InterviewInvitationEntity interviewInvitation){
        R info = sysServiceFegin.info();
        Map user = (Map) info.get("user");
        interviewInvitation.setUid(Integer.parseInt(user.get("userId").toString()));
		interviewInvitationService.save(interviewInvitation);

        return R.ok().put("id",interviewInvitation.getId());
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("interview:interviewinvitation:update")
    public R update(@RequestBody InterviewInvitationEntity interviewInvitation){
		interviewInvitationService.updateById(interviewInvitation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("interview:interviewinvitation:delete")
    public R delete(@RequestBody Long[] ids){
		interviewInvitationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 全部
     */
    @RequestMapping("/findAll")
    @RequiresPermissions("interview:interviewinvitation:findAll")
    public R findAll(){
        List<InterviewInvitationEntity> list = interviewInvitationService.findAll();

        return R.ok().put("list",list);
    }

    /**
     * 定时查询数据库，每半小时执行一次
     */
//    @Scheduled(cron = "0 0/30 * * * ?")
    //测试用的公式
//    @Scheduled(cron = "0/10 * * * * ?")
//    public void sendTimeMessage() {
//        Map<String, String> map = new HashMap<>();
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        List<InterviewInvitationEntity> list = interviewInvitationService.findAll();
//        for (InterviewInvitationEntity invitation : list) {
//            long between = DateUtil.between(invitation.getInterviewTime(), DateUtil.date(), DateUnit.HOUR);
//            if (between <= 3 && between > 0) {
//                map.put("phone", invitation.getPhone());
//                map.put("id", invitation.getId() + "");
//                String datetime = format.format(invitation.getInterviewTime());
//                String[] s = datetime.split(" ");
//                int i = 0;
//                for (String time : s) {
//                    if (i == 1) {
//                        map.put("time", time);
//                    }
//                    i = 1;
//                }
//                rabbitTemplate.convertAndSend("send_message", map);
//            }
//        }
//    }
}
